package com.wisezone.permission.service.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.wisezone.common.exception.WzServiceException;
import com.wisezone.common.exception.WzWebException;
import com.wisezone.common.service.impl.BaseServiceImpl;
import com.wisezone.permission.bean.form.WzMenuForm;
import com.wisezone.permission.bean.search.WzMenuSearch;
import com.wisezone.permission.dao.WzMenuDao;
import com.wisezone.permission.service.WzMenuService;
/**
 *
 *菜单; InnoDB free: 24576 kB
 *@author create by 
 *create date 2010-08-27 10:12:21 update date 2010-08-27 10:12:21
 *
 */
public class WzMenuServiceImpl extends BaseServiceImpl implements WzMenuService{

	 /**
	  *
	  *
	  *
	  */
	 private WzMenuDao wzMenuDao;


	 /**
	  * 获得菜单列表
	  * @param name 菜单名称
	  * @return
	  * @throws WzServiceException
	  */
	 public List<WzMenuForm> getMenuList(String name) throws WzServiceException{
		 
		 WzMenuSearch searchEntity = new WzMenuSearch();
		 
		 //获得一级菜单
		 searchEntity.setParentId(0L);
		 searchEntity.setName(name);
		 searchEntity.setEnableLike(true);
		 List<WzMenuForm> firstList = this.wzMenuDao.listFormAll(searchEntity);
		 
		 //获得非一级菜单
		 searchEntity = new WzMenuSearch();
		 searchEntity.setName(name);
		 searchEntity.setEnableLike(true);
		 searchEntity.setSqlClause("t1.parent_id > 0");
		 List<WzMenuForm> secondList = this.wzMenuDao.listFormAll(searchEntity);
		 
		 List<WzMenuForm> list = new ArrayList<WzMenuForm>();
		 if(firstList == null || firstList.size() == 0){
			 return list;
		 }
		 for(Iterator<WzMenuForm> it = firstList.iterator(); it.hasNext();){
			 WzMenuForm entity = it.next();
			 list.add(entity);
			 if(secondList == null || secondList.size() == 0){
			   continue;
			 }
			 for(Iterator<WzMenuForm> childIt = secondList.iterator(); childIt.hasNext();){
				 WzMenuForm childEntity = childIt.next();
				 if(childEntity.getParent() != null && childEntity.getParent().getId().equals(entity.getId())){
					 entity.addChild(childEntity);
				 }
			 }
		 }
		 return list;
	 }
	 
	 /**
	  * 获得父菜单
	  * @return
	  * @throws WzWebException
	  */
	 public List<WzMenuForm> getParentList() throws WzWebException{
		 
		 WzMenuSearch searchEntity = new WzMenuSearch();
		 searchEntity.addSqlClause("parent_id < 1");
		 List<WzMenuForm> list = this.listAll(searchEntity);
		 return list;
	 }
	 
	 


	 public void setWzMenuDao(WzMenuDao wzMenuDao){
		 this.setBaseDao(wzMenuDao);
		 this.wzMenuDao = wzMenuDao;
	 }



}